package com.example.lovedogs;

import com.example.lovedogs.fragment.FriendsFragment;
import com.example.lovedogs.fragment.GouquanFragment;
import com.example.lovedogs.fragment.ShopFragment;
import com.example.lovedogs.fragment.ZhuanyeFragment;
import com.example.lovedogs.fragment.ZizhenFragment;
import com.example.lovedogs.fragment.shop.ShangchengRightFragment;
import com.example.lovedogs.info.InfoActivity;
import com.example.lovedogs.mycenter.Address;
import com.example.lovedogs.mycenter.MenuMyPet;
import com.example.lovedogs.mycenter.ResideMenu;
import com.example.lovedogs.mycenter.ResideMenuInfo;
import com.example.lovedogs.mycenter.ResideMenuItem;
import com.example.lovedogs.mycenter.SettingActivity;
import com.example.lovedogs.mycenter.UserInfoActivity;
import com.example.lovedogs.util.DummyTabContent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener {
	//提交
	//提交333333333333333333333333
	//4444444444444
	//666666
	//77777777777777
	//jjjjjj
	class MyTabChangedListener implements OnTabChangeListener {

		@Override
		public void onTabChanged(String tabId) {
			FragmentManager fm = getSupportFragmentManager();
			fragment01 = (ZizhenFragment) fm.findFragmentByTag("zizhen");
			fragment02 = (ZhuanyeFragment) fm.findFragmentByTag("zhuanye");
			fragment03 = (GouquanFragment) fm.findFragmentByTag("gouquan");
			fragment04 = (FriendsFragment) fm.findFragmentByTag("friends");
			fragment05 = (ShopFragment) fm.findFragmentByTag("shop");
			ft = fm.beginTransaction();

			/** 如果存在Detaches掉 */
			if (fragment01 != null)
				ft.detach(fragment01);
			if (fragment02 != null)
				ft.detach(fragment02);
			if (fragment03 != null)
				ft.detach(fragment03);
			if (fragment04 != null)
				ft.detach(fragment04);
			if (fragment05 != null)
				ft.detach(fragment05);

			/** 判断当前选项卡是 */
			if (tabId.equalsIgnoreCase("zizhen")) {
				isTabZizhen();
				CURRENT_TAB = 1;
				tv_currenttab.setText(R.string.buttom_zizhen);
			}
			if (tabId.equalsIgnoreCase("zhuanye")) {
				isTabZhuanye();
				CURRENT_TAB = 2;
				tv_currenttab.setText(R.string.buttom_zhuanye);
			}
			if (tabId.equalsIgnoreCase("gouquan")) {
				isTabGouquan();
				CURRENT_TAB = 3;
				tv_currenttab.setText(R.string.buttom_gouquan);
			}
			if (tabId.equalsIgnoreCase("friends")) {
				isTabFriends();
				CURRENT_TAB = 4;
				tv_currenttab.setText(R.string.buttom_friends);
			}
			if (tabId.equalsIgnoreCase("shop")) {
				isTabShop();
				CURRENT_TAB = 5;
				tv_currenttab.setText(R.string.buttom_shop);
			}

			ft.commit();
		}

	}

	TabHost tabHost;
	TabWidget tabs;
	TextView tv_currenttab;
	ImageButton leftMenu;
	ImageButton imgBtn_info;

	ZizhenFragment fragment01;
	ZhuanyeFragment fragment02;
	GouquanFragment fragment03;
	FriendsFragment fragment04;
	ShopFragment fragment05;
	FragmentTransaction ft;
	RelativeLayout tabIndicator1, tabIndicator2, tabIndicator3, tabIndicator4,
			tabIndicator5;
	int CURRENT_TAB = 0;
	private ShangchengRightFragment rigthFragment;
	private ResideMenu resideMenu;
	
	private ResideMenuItem itemPet;
	private ResideMenuItem itemDingdan;
	private ResideMenuItem itemGouwuche;
	private ResideMenuItem itemShoucang;
	private ResideMenuItem itemAddress;
	private ResideMenuItem itemOther;
	
	private ResideMenuInfo info;
	
	public ShangchengRightFragment getRigthFragment() {
		return rigthFragment;
	}

	public void setRigthFragment(ShangchengRightFragment rigthFragment) {
		this.rigthFragment = rigthFragment;
	}

	@SuppressWarnings("deprecation")
	private void setUpMenu() {
		leftMenu = (ImageButton) findViewById(R.id.imgBtn_me);
		leftMenu.setOnClickListener(this);
		/*
		 * text1 = (TextView) findViewById(R.id.text1); text2 = (TextView)
		 * findViewById(R.id.text2); text3 = (TextView)
		 * findViewById(R.id.text3);
		 */
		// attach to current activity;
		resideMenu = new ResideMenu(this);
		resideMenu.setBackground(R.drawable.background1);
		resideMenu.attachToActivity(this);
		// resideMenu.setMenuListener(menuListener);
		// valid scale factor is between 0.0f and 1.0f. leftmenu'width is
		// 150dip.
		resideMenu.setScaleValue(0.6f);
		// 禁止使用右侧菜单
		resideMenu.setDirectionDisable(ResideMenu.DIRECTION_RIGHT);

		// create menu items;
		itemPet = new ResideMenuItem(this, R.drawable.ic_launcher, "宠物资料");
		itemDingdan = new ResideMenuItem(this, R.drawable.ic_launcher, "我的订单");
		itemGouwuche = new ResideMenuItem(this, R.drawable.ic_launcher, "购物车");
		itemShoucang = new ResideMenuItem(this, R.drawable.ic_launcher, "我的收藏");
		itemAddress = new ResideMenuItem(this, R.drawable.ic_launcher, "收货地址");
		itemOther = new ResideMenuItem(this, R.drawable.ic_launcher, "其他");

		resideMenu.addMenuItem(itemPet, ResideMenu.DIRECTION_LEFT);
		resideMenu.addMenuItem(itemDingdan, ResideMenu.DIRECTION_LEFT);
		resideMenu.addMenuItem(itemGouwuche, ResideMenu.DIRECTION_LEFT);
		resideMenu.addMenuItem(itemShoucang, ResideMenu.DIRECTION_LEFT);
		resideMenu.addMenuItem(itemAddress, ResideMenu.DIRECTION_LEFT);
		resideMenu.addMenuItem(itemOther, ResideMenu.DIRECTION_LEFT);
		info = new ResideMenuInfo(this, R.drawable.icon_profile, "请登录", "未知");

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv_currenttab = (TextView) findViewById(R.id.tv_currenttab);
		imgBtn_info = (ImageButton) findViewById(R.id.imgBtn_info);

		/*
		 * 
		 */
		
		imgBtn_info.setOnClickListener(this);

		// 导航栏设置
		findTabView();
		tabHost.setup();

		initTab();
		setUpMenu();//侧滑个人中心

		tabHost.setOnTabChangedListener(new MyTabChangedListener());
		tabHost.setCurrentTab(2);
		
		
		resideMenu.addMenuInfo(info);

		itemPet.setOnClickListener(this);
		itemDingdan.setOnClickListener(this);
		itemGouwuche.setOnClickListener(this);
		itemShoucang.setOnClickListener(this);
		itemAddress.setOnClickListener(this);
		itemOther.setOnClickListener(this);

		info.setOnClickListener(this);
		
	}

	private void initTab() {
		TabHost.TabSpec tSpecHome = tabHost.newTabSpec("zizhen");
		tSpecHome.setIndicator(tabIndicator1);
		tSpecHome.setContent(new DummyTabContent(getBaseContext()));
		tabHost.addTab(tSpecHome);

		TabHost.TabSpec tSpecWall = tabHost.newTabSpec("zhuanye");
		tSpecWall.setIndicator(tabIndicator2);
		tSpecWall.setContent(new DummyTabContent(getBaseContext()));
		tabHost.addTab(tSpecWall);

		TabHost.TabSpec tSpecCamera = tabHost.newTabSpec("gouquan");
		tSpecCamera.setIndicator(tabIndicator3);
		tSpecCamera.setContent(new DummyTabContent(getBaseContext()));
		tabHost.addTab(tSpecCamera);

		TabHost.TabSpec tSpecMessage = tabHost.newTabSpec("friends");
		tSpecMessage.setIndicator(tabIndicator4);
		tSpecMessage.setContent(new DummyTabContent(getBaseContext()));
		tabHost.addTab(tSpecMessage);

		TabHost.TabSpec tSpecMe = tabHost.newTabSpec("shop");
		tSpecMe.setIndicator(tabIndicator5);
		tSpecMe.setContent(new DummyTabContent(getBaseContext()));
		tabHost.addTab(tSpecMe);

	}

	private void findTabView() {
		// FrameLayout
		tabHost = (TabHost) findViewById(android.R.id.tabhost);
		// tabWidget = (TabWidget) findViewById(android.R.id.tabs);
		LinearLayout layout = (LinearLayout) tabHost.getChildAt(0);
		TabWidget tw = (TabWidget) layout.getChildAt(2);

		tabIndicator1 = (RelativeLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab1 = (TextView) tabIndicator1.getChildAt(1);
		ImageView ivTab1 = (ImageView) tabIndicator1.getChildAt(0);
		ivTab1.setBackgroundResource(R.drawable.selector_mood_home);
		tvTab1.setText(R.string.buttom_zizhen);

		tabIndicator2 = (RelativeLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab2 = (TextView) tabIndicator2.getChildAt(1);
		ImageView ivTab2 = (ImageView) tabIndicator2.getChildAt(0);
		ivTab2.setBackgroundResource(R.drawable.selector_mood_wall);
		tvTab2.setText(R.string.buttom_zhuanye);
		// tvTab2.setBackgroundResource(R.drawable.main_list_selector);

		tabIndicator3 = (RelativeLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab3 = (TextView) tabIndicator3.getChildAt(1);
		ImageView ivTab3 = (ImageView) tabIndicator3.getChildAt(0);
		ivTab3.setBackgroundResource(R.drawable.selector_mood_photograph);
		tvTab3.setText(R.string.buttom_gouquan);

		tabIndicator4 = (RelativeLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab4 = (TextView) tabIndicator4.getChildAt(1);
		ImageView ivTab4 = (ImageView) tabIndicator4.getChildAt(0);
		ivTab4.setBackgroundResource(R.drawable.selector_mood_message);
		tvTab4.setText(R.string.buttom_friends);

		tabIndicator5 = (RelativeLayout) LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, tw, false);
		TextView tvTab5 = (TextView) tabIndicator5.getChildAt(1);
		ImageView ivTab5 = (ImageView) tabIndicator5.getChildAt(0);
		ivTab5.setBackgroundResource(R.drawable.selector_mood_my_wall);
		tvTab5.setText(R.string.buttom_shop);

	}

	public void isTabZizhen() {
		if (fragment01 == null) {
			ft.add(R.id.realtabcontent, new ZizhenFragment(), "zizhen");
		} else {
			ft.attach(fragment01);
		}

	}

	public void isTabZhuanye() {
		if (fragment02 == null) {
			ft.add(R.id.realtabcontent, new ZhuanyeFragment(), "zhuanye");
		} else {
			ft.attach(fragment02);
		}

	}

	public void isTabGouquan() {
		if (fragment03 == null) {
			ft.add(R.id.realtabcontent, new GouquanFragment(), "gouquan");
		} else {
			ft.attach(fragment03);
		}

	}

	public void isTabFriends() {
		if (fragment04 == null) {
			ft.add(R.id.realtabcontent, new FriendsFragment(), "friends");
		} else {
			ft.attach(fragment04);
		}

	}

	public void isTabShop() {
		if (fragment05 == null) {
			ft.add(R.id.realtabcontent, new ShopFragment(), "shop");
		} else {
			ft.attach(fragment05);
		}

	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		//侧滑按钮中判断点击事件
		if (view == itemPet) {
			Intent intent = new Intent();
			intent.putExtra("flog", "宠物资料");
			intent.setClass(getApplicationContext(), MenuMyPet.class);
			startActivity(intent);
		} else if (view == itemDingdan) {
			Intent intent = new Intent();
			intent.putExtra("flog", "我的订单");
			intent.setClass(getApplicationContext(), SettingActivity.class);
			startActivity(intent);
		} else if (view == itemGouwuche) {
			Intent intent = new Intent();
			intent.putExtra("flog", "购物车");
			intent.setClass(getApplicationContext(), SettingActivity.class);
			startActivity(intent);
		} else if (view == itemShoucang) {
			Intent intent = new Intent();
			intent.putExtra("flog", "我的收藏");
			intent.setClass(getApplicationContext(), SettingActivity.class);
			startActivity(intent);
		} else if (view == itemAddress) {
			Intent intent = new Intent();
			intent.putExtra("flog", "收货地址");
			intent.setClass(getApplicationContext(), Address.class);
			startActivity(intent);
		} else if (view == itemOther) {
			Intent intent = new Intent();
			intent.putExtra("flog", "其他");
			intent.setClass(getApplicationContext(), SettingActivity.class);
			startActivity(intent);
		} else if (view == info) {
			Intent intent = new Intent();
			intent.putExtra("flog", "个人信息");
			intent.setClass(getApplicationContext(), UserInfoActivity.class);
			startActivity(intent);
		}
		
		
		
		switch (id) {
		case R.id.imgBtn_me:
			// 左侧调出个人中心
			resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
			break;
	
		case R.id.imgBtn_info:
			// 右侧划入消息
			Intent intent = new Intent(MainActivity.this, InfoActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}

	}
}
